package com.smartcampus.demo.controller;


import com.smartcampus.demo.domain.User;
import com.smartcampus.demo.service.UserService;
import com.smartcampus.demo.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping("/login")
    public Result<User> loginController(@RequestParam String username, @RequestParam String password){
        User user = userService.loginService(username, password);
        if(user!=null){
            return Result.success(user,"登录成功！");
        }else{
            return Result.error("123","账号或密码错误！");
        }
    }

    @PostMapping("/register")
    public Result<User> registerController(@RequestBody User newUser){
        System.out.println("1111"+newUser.getUsername());
        User user = userService.registerService(newUser);
        if(user!=null){
            return Result.success(user,"注册成功！");
        }else{
            return Result.error("456","用户名已存在！");
        }
    }


}
